clear
capture log close
set more on

set more 1

#delimit ;

log using wt,replace;

use 3BcarinstDrop25;

local potential_mkt "household";

drop ma1-ma9;

*** Generate index vectors ;

** Index vector for market (= combination year and market);
egen yema=group(year ma);

*** Drop models for which there is only 1 id in 1 year/market combination;

egen x = count(1), by(model);
drop if x==1;


* Generate mean and deviation from the mean;

* Deviation from the mean of all exogenous variables, princ, conditional market shares and the dependent variable;

local var "hp hpwe li eurokm fpinc size wi he nhome princ lsjdg lsdg lsjg lsj cpr lr";

foreach x in `var' {;
egen m`x'=mean(`x'), by(model);
gen d`x'=`x'-m`x';
};

drop mhpwe mhp mli mwi mprinc mnhome msize mcpr mlsjd mlsdg mlsj;

local year "year";

foreach x in `year'{;
tab year, gen(year`y');
forvalues y = 1/9{;
egen m`x'`y'=mean(`x'`y'), by (model);
gen d`x'`y'=year`y'-m`x'`y';
};
};
drop myear*;

local country "ma";

foreach x in `country'{;
tab ma, gen(ma`y');
forvalues y = 1/9{;
egen m`x'`y'=mean(`x'`y'), by (model);
gen d`x'`y'=`x'`y'-m`x'`y';
};
};

drop mma*;


**** TREND;
gen time=year-1997;

gen time1=time*ma1;
gen time2=time*ma2;
gen time3=time*ma3;
gen time4=time*ma4;
gen time5=time*ma5;
gen time6=time*ma6;
gen time7=time*ma7;
gen time8=time*ma8;
gen time9=time*ma9;

gen time21=time1^2;
gen time22=time2^2;
gen time23=time3^2;
gen time24=time4^2;
gen time25=time5^2;
gen time26=time6^2;
gen time27=time7^2;
gen time28=time8^2;
gen time29=time9^2;

gen time31=time1^3;
gen time32=time2^3;
gen time33=time3^3;
gen time34=time4^3;
gen time35=time5^3;
gen time36=time6^3;
gen time37=time7^3;
gen time38=time8^3;
gen time39=time9^3;


* Trend;

local var " time1	time2	time3	time4	time5	time6	time7	time8	time9		
		time21	time22	time23	time24	time25	time26	time27	time28	time29 
		time31	time32	time33	time34	time35	time36	time37	time38	time39";

foreach x in `var' {;
egen m`x'=mean(`x'), by(model);
gen d`x'=`x'-m`x';
};

drop mtime*;


* Deviation from the mean of multiplicative dummy variables *;

local help "yema";

foreach x in `help'{;
qui tab yema, gen(yema`y');
forvalues y = 1/81{;
egen m`x'`y'=mean(`x'`y'), by (model);
gen d`x'`y'=`x'`y'-m`x'`y';
};
};
drop myema*;

* month of introduction dummy;

drop md*; rename  market_duration md; label variable md "number of months the car is in the market";

local md "md";

foreach x in `md'{; 	
tab `md', gen(md`y');
forvalues y = 1/12{;
egen m`x'`y'=mean(`x'`y'), by (model);
gen d`x'`y'=md`y'-m`x'`y';
};
};

drop mmd*;

* Logit instruments *;

local inst " i_hp  i_hpwe	i_li  i_eurokm i_fpinc   i_size i_wi  i_he  i_nhome i_num
		 if_hp if_hpwe	if_li if_eurokm if_fpinc if_size if_wi if_he if_nhome if_num";

foreach x in `inst' {;
egen m`x'=mean(`x'), by(model);
gen d`x'=`x'-m`x';
};

* One level nested logit instruments *;

local 1nl_inst  	" is_hp  is_hpwe  is_li  is_eurokm is_fpinc 	 is_wi  is_he is_nhome is_num
			  ifs_hp ifs_hpwe ifs_li ifs_eurokm ifs_fpinc ifs_wi ifs_he ifs_nhome ifs_num
			";

foreach x in `1nl_inst' {;
egen m`x'=mean(`x'), by(model);
gen d`x'=`x'-m`x';
};


* Two level nested logit instruments *;

local 2nl_inst "	  ids_hp  ids_hpwe  ids_li ids_eurokm   ids_fpinc  ids_wi  ids_he ids_nhome ids_num
       		  ifds_hp ifds_hpwe ifds_li ifds_eurokm ifds_fpinc ifds_wi ifds_he ifds_nhome ifds_num
       	   ";

foreach x in `2nl_inst' {;
egen m`x'=mean(`x'), by(model);
gen d`x'=`x'-m`x';
};
rename ifds_hpwe ifdshw; 

/*

* One level nested logit instruments flexible *;

local 1nl_instfl	  "is_hp1 is_hp2 is_hp3 is_hp4 is_hp5 is_hp6 is_hp7  
			   is_li1 is_li2 is_li3 is_li4 is_li5 is_li6 is_li7
			   is_eurokm1 is_eurokm2 is_eurokm3 is_eurokm4 is_eurokm5 is_eurokm6 is_eurokm7
			   is_fpinc1 is_fpinc2 is_fpinc3 is_fpinc4 is_fpinc5 is_fpinc6 is_fpinc7 
			   is_wi1 is_wi2 is_wi3 is_wi4 is_wi5 is_wi6 is_wi7 
			   is_he1 is_he2 is_he3 is_he4 is_he5 is_he6 is_he7  
			   is_num1 is_num2 is_num3 is_num4 is_num5 is_num6 is_num7 
       	   	  ";

foreach x in `1nl_instfl' {;
egen m`x'=mean(`x'), by(model);
gen d`x'=`x'-m`x';
};

* Two level nested logit instruments flexible *;

local 2nl_instfl	  "	ids_hp_class1 ids_hp_class2 ids_hp_class3 ids_hp_class4 ids_hp_class5 ids_hp_class6 ids_hp_class7
				ids_li_class1 ids_li_class2 ids_li_class3 ids_li_class4 ids_li_class5 ids_li_class6 ids_li_class7
		  		ids_eurokm_class1 ids_eurokm_class2 ids_eurokm_class3 ids_eurokm_class4 ids_eurokm_class5 ids_eurokm_class6 ids_eurokm_class7 
				ids_fpinc_class1 ids_fpinc_class2 ids_fpinc_class3 ids_fpinc_class4 ids_fpinc_class5 ids_fpinc_class6 ids_fpinc_class7 
				ids_wi_class1 ids_wi_class2 ids_wi_class3 ids_wi_class4 ids_wi_class5 ids_wi_class6 ids_wi_class7
		  		ids_he_class1 ids_he_class2 ids_he_class3 ids_he_class4 ids_he_class5 ids_he_class6 ids_he_class7
		 		ids_num_class1 ids_num_class2 ids_num_class3 ids_num_class4 ids_num_class5 ids_num_class6 ids_num_class7
       	   	  ";

foreach x in `2nl_instfl' {;
egen m`x'=mean(`x'), by(model);
gen d`x'=`x'-m`x';
};

*** I rename some variables just for gauss;

renvars 	dids_hp_class1	dids_hp_class2 	dids_hp_class3	dids_hp_class4	dids_hp_class5 	dids_hp_class6	dids_hp_class7 /
		didshpc1 		didshpc2 		didshpc3 		didshpc4 		didshpc5 		didshpc6		didshpc7;
renvars 	dids_li_class1	dids_li_class2 	dids_li_class3	dids_li_class4	dids_li_class5 	dids_li_class6	dids_li_class7 /
		didslic1 		didslic2 		didslic3 		didslic4 		didslic5 		didslic6		didslic7;
renvars 	dids_eurokm_class1	dids_eurokm_class2 	dids_eurokm_class3	dids_eurokm_class4	dids_eurokm_class5 	dids_eurokm_class6	dids_eurokm_class7 /
		didseurokmc1 		didseurokmc2 		didseurokmc3 		didseurokmc4 		didseurokmc5 		didseurokmc6		didseurokmc7;
renvars 	dids_fpinc_class1		dids_fpinc_class2 	dids_fpinc_class3		dids_fpinc_class4		dids_fpinc_class5 	dids_fpinc_class6	dids_fpinc_class7 /
		didsfpincc1 		didsfpincc2 		didsfpincc3 		didsfpincc4 		didsfpincc5 		didsfpincc6		didsfpincc7;
renvars 	dids_wi_class1	dids_wi_class2 	dids_wi_class3	dids_wi_class4	dids_wi_class5 	dids_wi_class6	dids_wi_class7 /
		didswic1 		didswic2 		didswic3 		didswic4 		didswic5 		didswic6		didswic7;
renvars 	dids_he_class1	dids_he_class2 	dids_he_class3	dids_he_class4	dids_he_class5 	dids_he_class6	dids_he_class7 /
		didshec1 		didshec2 		didshec3 		didshec4 		didshec5 		didshec6		didshec7;
renvars 	dids_num_class1	dids_num_class2 	dids_num_class3	dids_num_class4	dids_num_class5 	dids_num_class6	dids_num_class7 /
		didsnuc1 		didsnuc2 		didsnuc3 		didsnuc4 		didsnuc5 		didsnuc6		didsnuc7;
renvars    	lsjdg_CLASS1 	lsjdg_CLASS2	lsjdg_CLASS3 	lsjdg_CLASS4 	lsjdg_CLASS5	lsjdg_CLASS6 	lsjdg_CLASS7 /
		lsjdgcl1		lsjdgcl2		lsjdgcl3		lsjdgcl4		lsjdgcl5		lsjdgcl6		lsjdgcl7;

renvars    	dlsjdg_CLASS1 	dlsjdg_CLASS2	dlsjdg_CLASS3 	dlsjdg_CLASS4 	dlsjdg_CLASS5	dlsjdg_CLASS6 	dlsjdg_CLASS7 /
		dlsjdgc1		dlsjdgc2		dlsjdgc3		dlsjdgc4		dlsjdgc5		dlsjdgc6		dlsjdgc7;
*/;
* Index vector for market *;

egen NDnummarket=group(yema);

** Index vector for subsegment;

sort Fclass nhome;
egen SDnum=group(Fclass nhome);

*** Helpers to calculte market shares;

*** Very important: you need to sort before;

sort yema Fclass SDnum model;

gen temp2=Fclass if SDnum~=SDnum[_n+1];
tab temp2, gen (temp2_);
qui mvencode temp2_*, mv(0)  override;
sort yema Fclass SDnum model;

gen temp3=NDnummarket if Fclass~=Fclass[_n+1];
tab temp3, gen (temp3_);
qui mvencode temp3_*, mv(0)  override;

order  year ma model model_group Fclass body firm princ q date cy hp li le wi he we acc speed abs air airbag brand fuel yema yema* dyema*;


compress;
log close;
saveold WT, replace;
